package com.zaicheng.cloud;

import com.zaicheng.cloud.netty.client.NettyClient;
import com.zaicheng.cloud.remotecall.RemoteCall;
import com.zaicheng.cloud.remotecallclient.DefaultTargeter;
import com.zaicheng.cloud.remotecallclient.Targeter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 @author zaicheng
 @version 1.0
 @date 2022-04-19
 @email zzc15847256473@163.com
 */

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({RemoteCall.class})
@EnableConfigurationProperties(RemoteCallClientProperties.class)
public class RemoteCallAutoConfiguration {

    @Autowired(required = false)
    private List<RemoteCallClientSpecification> specifications = new ArrayList<>();

    public RemoteCallAutoConfiguration() {
    }

    @Bean
    public  RemoteCallContext  remoteCallContext(){
        RemoteCallContext callContext = new RemoteCallContext();
        return  callContext;
    }

    @Configuration(proxyBeanMethods = false)
    protected  static  class  NettyRemoteCallConfiguration{

        @Bean
        @ConditionalOnMissingBean(Client.class)
        public  Client  remoteCallClient(){
            return  new NettyClient();
        }
    }

    @Configuration(proxyBeanMethods = false)
    protected  static  class  DefaultRemoteCallTargeterConfiguration{
        public DefaultRemoteCallTargeterConfiguration() {
        }

        @Bean
        @ConditionalOnMissingBean
        public Targeter remoteCallTargeter(){
            return  new DefaultTargeter();
        }

    }
}
